/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package testCase;

/*
 * 	@release Final Release
 *	@author  Lee Cheuk Wang
 *	@version 1.2.1
 */

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;

import junit.framework.TestCase;

import org.junit.Test;

import csBakery.LogController;

public class LogControllerTest  extends TestCase{
	
	private LogController lc;
	private String today;
	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private String separator;
	private final String logPath = "test\\testlog.txt";

	
	public void setUp(){	
		//Prepare String for the testing Date
		Date testDate = new Date();
		DateFormat dateformat = DateFormat.getDateInstance(DateFormat.SHORT);
		today = dateformat.format(testDate);
		
		//Set up for output stream testing
		System.setOut(new PrintStream(outContent));
		separator = System.getProperty("line.separator");
		
		//Test Log with a different Log file
		lc = new LogController(logPath);
	}
	
	public void tearDown()	{
		System.setOut(null);
		
		PrintWriter writer;
		try {
			writer = new PrintWriter(logPath);
		} catch (FileNotFoundException e) {
			writer = null;
		}
		if (writer!=null){
			writer.print("");
			writer.close();
		}
	}
	
	@Test
	//Test addLog function for stocking
	public void testAddLogCase1() throws FileNotFoundException, IOException{				
		lc.addLog("Bagel", "STOCK", 50);
		lc.saveLog();
		lc = new LogController(logPath);
		assertEquals("Bagel STOCK 50 " + today,getLastLine());
	}

	@Test
	//Test addLog function for selling
	public void testAddLogCase2() throws FileNotFoundException, IOException{				
		lc.addLog("Barbari Bread", "SALE", 20);
		lc.saveLog();
		lc = new LogController(logPath);
		assertEquals("Barbari Bread SALE 20 " + today,getLastLine());
	}

	@Test
	//Test printLog function
	public void testPrintLog(){
		String output = "";
		output += "Bagel STOCK 50 " + today + "\n";
		output += "Baguette SALE 20 " + today + "\n" + separator;
		
		lc.addLog("Bagel", "STOCK", 50);
		lc.addLog("Baguette", "SALE", 20);
		lc.saveLog();
		lc = new LogController(logPath);
		lc.printLog();
		assertEquals(output, outContent.toString());
	}
	
	private String getLastLine() throws FileNotFoundException, IOException {	
		//Prepare Log file
		File file = new File(logPath);
		FileInputStream f_input = new FileInputStream(file);
		DataInputStream d_input = new DataInputStream(f_input);
		BufferedReader reader = new BufferedReader(new InputStreamReader(d_input));
		
		String currentLine = reader.readLine();
		String nextLine = reader.readLine();
		
		while (null != currentLine){
		    if (null == nextLine){
		        //last line, cause there's nothing else coming up
		    	return currentLine;
		    }
		    currentLine = nextLine;
		    nextLine = reader.readLine();
		}
		return null;
	}
}
